package com.sasu.university_data.service.impl;


import com.sasu.university_data.entity.Info;
import com.sasu.university_data.entity.Major;
import com.sasu.university_data.entity.MajorInfo;
import com.sasu.university_data.mapper.InfoMapper;
import com.sasu.university_data.mapper.MajorMapper;
import com.sasu.university_data.service.MajorService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("majorService")
public class MajorServiceImpl implements MajorService {
    @Resource
    private MajorMapper majorMapper;
    @Resource
    private InfoMapper infoMapper;


    @Override
    public List<Major> queryById( Integer id) {
        return this.majorMapper.queryById(id);
    }

    @Override
    public long count (String name) {
        return majorMapper.count(name);
    }

    @Override
    public long count() {
        return majorMapper.countNo();
    }

    @Override
    public List<Major> selectByPage (String name, Integer page, Integer pageSize ) {
        if(page<=1) {
            page = 1;
        }
        if(pageSize<=0) {
            pageSize = 10;
        }
        return majorMapper.selectByPage(name,(page - 1) * pageSize, pageSize);
    }

    @Override
    public boolean insert ( Major major ) {
        return majorMapper.insert(major)>0;
    }

    @Override
    public boolean update ( Major major ) {
        return majorMapper.update(major)>0;
    }

    @Override
    public boolean deleteById ( Integer mId ) {
        return majorMapper.deleteById(mId)>0;
    }

    @Override
    public boolean deleteBymIds ( int[] Ids ) {
        return majorMapper.deleteByIds(Ids)>0;
    }

    @Override
    public List<MajorInfo> selectAllByPage(Integer page, Integer pageSize) {
        if (page <= 1) {
            page = 1;
        }
        if (pageSize <= 0) {
            pageSize = 10;
        }
        List<Major> majors = majorMapper.selectAllByPage((page - 1) * pageSize, pageSize);
        List<MajorInfo> ma = new ArrayList<>();

//        System.out.println(majors.size());

        MajorInfo mas = null;
        for (Major m : majors) {
            mas = new MajorInfo();
            Info info = infoMapper.selectByIdInfo(m.getId());
            if (info!=null) {
//                System.out.println("info:" + info);
                mas.setId(info.getId());
                mas.setAddress(info.getAddress());
                mas.setName(m.getName());
                mas.setLevel2(m.getLevel2());
                mas.setLevel1(m.getLevel1());
                mas.setMId(m.getMId());
                mas.setMNum(m.getMNum());
                mas.setCollegeName(info.getName());
                ma.add(mas);
            }
        }

        return ma;
    }

}
